package com.hongyu.smart.village.util.bluetooth;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;

import com.hongyu.smart.village.config.Constant;

import java.util.Set;

public class BlueToothUtils {
    private static BlueToothUtils instance =new BlueToothUtils();
    public   BluetoothAdapter mBluetoothAdapter ;





  public boolean supportBlueTooth(){
      mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      if (mBluetoothAdapter==null){
          return false;
      }
      return true;
  }
  @SuppressLint("MissingPermission")
  public void openBluetooth(Activity activity){
      if (!mBluetoothAdapter.isEnabled()) {
          Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
          activity.startActivityForResult(intent, Constant.ENABLE_BLUETOOTH_REQUEST_CODE);
      }
  }



    public static BlueToothUtils getInstance() {
        if(instance==null){
            instance=new BlueToothUtils();
        }
        return instance;
    }


    @SuppressLint("MissingPermission")
    public void startDiscovery() {
      mBluetoothAdapter.startDiscovery();

    }

    @SuppressLint("MissingPermission")
    public void  cancelDiscovery() {
        if(mBluetoothAdapter.isDiscovering()) {
            mBluetoothAdapter.cancelDiscovery();
        }
    }
    @SuppressLint("MissingPermission")
    public Set<BluetoothDevice> getBondedDevices() {
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        return pairedDevices;

    }

    @SuppressLint("MissingPermission")
    public void contentDevice(BluetoothDevice bluetoothDevice) {
      cancelDiscovery();

    }

    private void connectBluetooth(String mac) {
//        showLoading();
//        //连接打印机
//        PrinterDevices blueTooth = new PrinterDevices.Build()
//                .setContext(this)
//                .setConnMethod(ConnMethod.BLUETOOTH)
//                .setMacAddress(mac)
//                .setCommand(Command.CPCL)
//                .setCallbackListener(this)
//                .build();
//        // TODO 连接设备
//        Printer.connect(blueTooth);
    }
    private void initBluetooth() {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//        if (mBluetoothAdapter == null) {
//            toast("该设备不支持蓝牙");
//        } else {
//            //检测蓝牙未开启，主动开启
//            if (!mBluetoothAdapter.isEnabled()) {
//                Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//                startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
//            } else {
//                manager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
//                //检测定位是否开启
//                if (EasyPermissions.hasPermissions(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
//                    if ((Build.VERSION.SDK_INT >= 29) && !manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//                        gpsDialog();
//                    } else {
//                        searchBlueTooth();
//                    }
//                } else {
//                    gpsDialog();
//                }
//            }
//        }
    }


    public void startPrintDate() {
//        if(mTotal<1){
//            toast("打印模板数据为空");
//            return;
//        }
//        ThreadPoolManager.getInstance().addTask(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    if (Printer.getPortManager() == null) {
//                        sendHandlerMsg(WHAT_PRINT_NOT_CONNECTED,0);
//                        return;
//                    }
//                    //****************** 查询状态********************
//                    Command command = Printer.getPortManager().getCommand();
//                    int status = Printer.getPrinterState(command);
//                    if (status != 0) {
//                        //打印机异常状态
//                        Log.e("OkGo-print","---------打印机状态status="+status+"------mIndex="+mIndex);
//                        sendHandlerMsg(WHAT_PRINT_STATUS,status);
//                        return;
//                    }
//                    //Thread.sleep(1500);
//                    //******************调用打印*********************
//                    Vector<Byte> data;
//                    if(mIndex<mTotal){
//                        if(mPrintData.get(mIndex).getType()==1){
//                            //客户联标签
//                            data =PrintContent.getCustomerTag(PrintMainActivity.this,mPrintData.get(mIndex));
//                        }else {
//                            //货物标签
//                            data =PrintContent.getGoodsTag(PrintMainActivity.this, mPrintData.get(mIndex),mIndex,mTotal-1 );
//                        }
//                        boolean result = Printer.getPortManager().writeDataImmediately(data);
//                        //发送结果
//                        Log.e("OkGo-print","---------打印进度mIndex="+mIndex+"-----"+result);
//                        sendHandlerMsg(WHAT_PRINT_RESULT,result?0:1);
//                    }
//                } catch (Exception e) {
//                    //发送异常
//                    Log.e("OkGo-print","---------打印出现异常");
//                    sendHandlerMsg(WHAT_PRINT_FAIL,0);
//                }
//            }
//        });
    }
}
